﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.MTHControlLib
{
    public partial class CheckBoxEx : CheckBox
    {
        public CheckBoxEx()
        {
            InitializeComponent();
            //文本纵向居中
            stringFormat.LineAlignment = StringAlignment.Center;
            //文本横向居中
            stringFormat.Alignment = StringAlignment.Center;

            //刷缓存，防止闪烁
            //设置组件的样式 解决闪烁的问题，和背景问题
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);//此设置确保控件的所有绘制都通过 WM_PAINT 消息处理。这有助于避免绘制不一致和闪烁问题，
            this.SetStyle(ControlStyles.DoubleBuffer, true);//双缓冲是一种减少或消除闪烁的技术，它通过在内存中绘制控件的副本，然后再一次性将其绘制到屏幕上，从而提高了绘制的平滑性。
            this.SetStyle(ControlStyles.ResizeRedraw, true);//当控件大小改变时，此设置确保控件会重绘其内容。这有助于避免在调整控件大小时出现绘制问题，如未更新的区域或闪烁。
            this.SetStyle(ControlStyles.Selectable, true);//此设置使控件可选择。对于某些控件（如自定义控件），可能需要明确设置此标志以启用选择功能。
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //此设置允许控件支持透明背景色。当控件的背景色设置为 Color.Transparent 时，它将显示其父容器的背景。
        }

        private StringFormat stringFormat = new StringFormat();

        private Color checkColor = Color.FromArgb(3, 25, 66);
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("选择框的颜色")]
        public Color CheckColor
        {
            get { return checkColor; }
            set
            {
                checkColor = value;
                this.Invalidate();
            }
        }

        private Color checkBackgroundColor = Color.White;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("选择框的背景颜色")]
        public Color CheckBackgroundColor
        {
            get { return checkBackgroundColor; }
            set
            {
                checkBackgroundColor = value;
                this.Invalidate();
            }
        }

        private float checkWidth = 20f;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("选择框的宽度")]
        public float CheckWidth
        {
            get { return checkWidth; }
            set
            {
                checkWidth = value;
                this.Invalidate();
            }
        }

        private string checkTitle = "1#站点的温度";
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("选择框的名称")]
        public string CheckTitle
        {
            get { return checkTitle; }
            set
            {
                checkTitle = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //绘制控件的颜色
            base.OnPaintBackground(e);

            Graphics graphics = e.Graphics;

            

            //消除锯齿
            //要实现抗锯齿效果
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //绘制的文本将尝试利用ClearType效果（如果显示设备支持），从而使文本在LCD屏幕上看起来更加清晰和易读。
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            //绘制选择框和文本框
            Rectangle checkRectangle;

            Rectangle textRectangle;

            drawRectangle(out checkRectangle, out textRectangle);
            //绘制实心选择矩形框
            SolidBrush solidBrush = new SolidBrush(checkBackgroundColor);
            graphics.FillRectangle(solidBrush, checkRectangle);
            Pen pen = new Pen(Color.LightCyan, 1f);
            graphics.DrawRectangle(pen, checkRectangle);//选择框的边框

            //绘制文本框
            graphics.DrawString(checkTitle, this.Font, new SolidBrush(this.ForeColor), textRectangle, stringFormat);

            //选择
            if (this.CheckState == CheckState.Checked) 
            {
                drawCheck(graphics, checkRectangle, checkColor);
            }


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="checkRectangle"></param>
        /// <param name="checkColor"></param>
        private void drawCheck(Graphics graphics, Rectangle checkRectangle, Color checkColor)
        {
            PointF[] pointFs = new PointF[3];
            pointFs[0] = new PointF(checkRectangle.X + checkRectangle.Width / 4.5f, checkRectangle.Y + checkRectangle.Height / 2.5f);
            pointFs[1] = new PointF(checkRectangle.X + checkRectangle.Width / 2.5f, checkRectangle.Bottom - checkRectangle.Height / 3.0f);
            pointFs[2] = new PointF(checkRectangle.Right - checkRectangle.Width / 4.0f, checkRectangle.Y + checkRectangle.Height / 4.5f);
            Pen pen = new Pen(checkColor, 2.0f);
            graphics.DrawLines(pen, pointFs);

        }

        private void drawRectangle(out Rectangle checkRectangle, out Rectangle textRectangle)
        {
            checkRectangle = new Rectangle(3, Convert.ToInt32((this.Height - checkWidth) * 0.5f), Convert.ToInt32(checkWidth), Convert.ToInt32(checkWidth));
            textRectangle = new Rectangle(checkRectangle.Right + 3, 2, Convert.ToInt32(this.Width - checkWidth - 6), this.Height);
        }
    }
}
